/**
 * 
 */
package com.jm.utils;

import com.jm.web.BasePage;
import com.jm.web.data.BaseDataEntity;
import com.jm.web.form.Button;
import com.jm.web.tags.table.TableTag;

/**
 * @author yanjie
 *
 * 2011/10/12 上午9:14:02
 */
public class PageJsGear {
	/**
	 * @param page
	 * @return
	 * 獵取頁面標題的js ,用來隱藏north toolbar
	 */
	public static String getPageTitleButtonJs(BasePage page){
		StringBuilderEx sb = new StringBuilderEx();
		if(page.isBorderLayout()){
			sb.Append("try{$P.layout['page'].toggle('north');$P.refresh();}catch(e){return false;}return false;");
		}else if(page.isNormalLayout()){
			sb.Append("$P.toggle('north');$P.refresh();return false;");
		}else{
			sb.Append("return false;");
		}
		
		return sb.toString();
	}
	
	public static void registerPageLayoutJs(BasePage page){
		if(page.isBorderLayout()){
			registerPageBorderLayoutJs(page);
		}else if(page.isNormalLayout()){
			registerPageNormalLayoutJs(page);
		}
	}
	
	/**
	 * @param page
	 * @return
	 * border 佈局js
	 */
	public static void registerPageBorderLayoutJs(BasePage page){
		StringBuilderEx sb = new StringBuilderEx();
		sb.Append("\r\n	var layout = $('body').layout({");
		sb.Append(page.getLayoutConfig().toString());
		sb.Append("\r\n	});");
		sb.Append("\r\n	$P.layout['page'] = layout ;");
		sb.Append("\r\n	delete layout ;");
		page.RegisterOnReadyScript(BasePage.SCRIPTRUN_CONFIG,sb.toString());
	}
	
	/**
	 * @param page
	 * @return
	 * normal 佈局js
	 */
	public static void registerPageNormalLayoutJs(BasePage page){
		StringBuilderEx sb = new StringBuilderEx();
		sb.Append("<div onclick=\"$P.toggle('north');return false;\" id=\"north-resizer\" class=\"north-toggler1\" title=\"Slide Open\"><div id=\"north-toggler\" class=\"north-toggler2\" title=\"Open\"></div></div>");
		page.RegisterOnReadyScript(BasePage.SCRIPTRUN_HTML,sb.toString());
	}
	
	/**
	 * @param page
	 * @return
	 * 表格js
	 */
	public static String getTableJs(BasePage page,TableTag tableTag){
		if(page.isFastMode()) return "";
		StringBuilderEx sb = new StringBuilderEx();
		if(tableTag.GetExtValue("stripe",false) || tableTag.GetExtValue("slip",false) || tableTag.GetExtValue("select",false)){
			sb.Append("var _trs = $(\"table[id=%1$s] > tbody > tr\");",tableTag.getTable().getId());
		}
		//隔行变色
		if(tableTag.GetExtValue("stripe",false)){
			sb.Append("$(\"table[id=%1$s] > tbody > tr:even[stripe!=false]\").addClass(\"tr-darkstrip\");",tableTag.getTable().getId());
			sb.Append("$(\"table[id=%1$s] > tbody > tr:odd[stripe!=false]\").addClass(\"tr-lightstrip\");",tableTag.getTable().getId());
		}
		
		//鼠标事件
//		if(!tableTag.IsContainsKey("")){
//			sb.Append("if(_trs.length <= 100)_trs.mouseover(function(){$(this).addClass(\"tr-over\");});");
//			sb.Append("if(_trs.length <= 100)_trs.mouseout(function(){$(this).removeClass(\"tr-over\");});");
//		}
			
		if(tableTag.GetExtValue("slip",false)){
			sb.Append("_trs.mouseover(function(){$(this).addClass(\"tr-over\");});");
			sb.Append("_trs.mouseout(function(){$(this).removeClass(\"tr-over\");});");
		}
		
		if(tableTag.GetExtValue("select",false)){
			sb.Append("_trs.click(function(){$(\"table[id=%1$s] > tbody > tr.tr-selected\").removeClass(\"tr-selected\");$(this).addClass(\"tr-selected\");});",tableTag.getTable().getId());
			sb.Append("_trs.dblclick(function(){$(\"table[id=%1$s] > tbody > tr.tr-selected\").removeClass(\"tr-selected\");});",tableTag.getTable().getId());
		}
		//处理ID div 宽度100% 加 padinng 的bug
//		sb.Append("if($.browser.msie != undefined && $.browser.msie == true ){");
//			sb.Append("$('div.header,div.footer').css('width', '100%');");
//			sb.Append("$('div.data').css('width', '100%');");
//			sb.Append("$('form').css('width', '100%');	");
//		sb.Append("}else{");
//			//设置表头,表尾 右边距
//			sb.Append("$('div.header, div.footer').css('paddingRight', $.layout.scrollbarWidth() +'px');");
//		sb.Append("}");
		return sb.toString();
	}
	
	/**
	 * @param page
	 * @return
	 * 工具欄按鈕js
	 */
	public static String getToolbarButtonJs(BasePage page){
		StringBuilderEx sb = new StringBuilderEx();
		String strDisabledJs = "var _enab=$(this).attr('enab');if(_enab== 'false')return false;";
		String strSelect = "table[type=button] " ;
		sb.Append("$( \"%1$s\", \"div#north\" ).mouseenter(function(){",strSelect);
		sb.Append("%1$s",strDisabledJs);
		sb.Append("killAllMenusExceptId($(this).gv('id'));");
		sb.Append("$(this).addClass(\"ax-btn-over\");");
		sb.Append("});");
		
		sb.Append("$( \"%1$s\", \"div#north\" ).mouseleave(function(){",strSelect);
			sb.Append("%1$s",strDisabledJs);
			//sb.Append("$(this).data('movein',false);$(this).removeClass(\"ax-btn-over\"); if(!($(this).attr('id') == 'filter' && $('#%1$s:visible').length > 0))$(this).removeClass(\"ax-btn-pressed\");",page.getSpId());
			sb.Append("$(this).removeClass(\"ax-btn-over\");");
		sb.Append("});");
		sb.Append("");
		
		sb.Append("$( \"%1$s\", \"div#north\" ).mousedown(function(){",strSelect);
		sb.Append("%1$s",strDisabledJs);
			sb.Append("$(this).addClass(\"ax-btn-pressed\");");
		sb.Append("});");
	
		sb.Append("$( \"%1$s\", \"div#north\" ).mouseup(function(){",strSelect);
			sb.Append("%1$s",strDisabledJs);
			sb.Append("$(this).removeClass(\"ax-btn-pressed\");");
			//sb.Append("$(this).removeClass(\"ax-btn-pressed\"); if($('#%1$s:visible').length == 0){$(this).addClass(\"ax-btn-pressed\");}",page.getSpId());
		sb.Append("});");
		
//		sb.Append("$('#filter').click(function(){console.log($('#filter').attr('class'));if($('#filter').attr('class').indexOf('ax-btn-pressed') > 0){$('#filter').removeClass('ax-btn-pressed');}else{$('#filter').addClass('ax-btn-pressed');}});");
//		sb.Append("$('#filter').mouseup(function(){$(this).toggleClass('ax-btn-pressed');});");
		
		return sb.toString();
	}
	
	/**
	 * @param page
	 * 工具栏Filter 按钮js
	 */
	public static void registerToolbarFilterJs(BasePage page){
		StringBuilderEx sb = new StringBuilderEx();
		if(!page.isSpOpen()){
			sb.Append("$('div[id=%1$s]').toggle();$P.refresh();",page.getSpId());
		}
		page.RegisterOnReadyScript(BasePage.SCRIPTRUN_ONREADY, sb.toString());
		
		sb.Reset();
		sb.Append("$('table[id=toolbar] td[id=filter-btn]').click(function(){$('div[id=%1$s]').toggle();$P.refresh();return false;});",page.getSpId());
		page.RegisterOnReadyScript(BasePage.SCRIPTRUN_ONREADY, sb.toString());
		
	}
	
	/**
	 * @param page
	 * @param button
	 * @return
	 * 工具欄按钮菜單js
	 */
	public static String getToolbarMenuJs(BasePage page,Button button){
		StringBuilderEx sb = new StringBuilderEx();
		sb.Append("$('table[id=%1$s]').jmmenu({",button.getId());
		sb.Append("content:$('div[id=%1$s-option]').html()",button.getId());
		sb.Append(",flyOut: true");
		sb.Append(",btnid: '%1$s'",button.getId());
		if(!StringHelper.IsNullOrEmpty(button.getMenuWidth()))
			sb.Append(",width:'%1$s'",button.getMenuWidth());
		sb.Append("});");
		return sb.toString();
	}
	
	/**
	 * @param page
	 * @return
	 * 表單submitjs
	 */
	public static String getFormSubmitJs(BasePage page){
		StringBuilderEx sb = new StringBuilderEx();
		sb.Append("$('form').submit(function(){if($('action').val() == ''){return false;}else{return true;}});");
		return sb.toString();
	}
	
	public static void registerTableRowData(BasePage page,String tabId,int index,BaseDataEntity row,String params){
		StringBuilderEx sb = new StringBuilderEx();
		if(!page.gridDatacontainsKey(tabId)){
			page.addGridData(tabId);
			sb.Append("if(!$P.griddata['%1$s'])$P.griddata['%1$s']={};",tabId);
		}
		BaseDataEntity tmp = null ;
		if(StringHelper.Compare("all",params,true)==0){
			tmp = row;
		}else{
			tmp = new BaseDataEntity();
			row.CopyTo(tmp,params,true);	
		}
		sb.Append("$P.griddata['%1$s']['%2$s']=%3$s;",tabId,String.valueOf(index),tmp.ToJSONString());
		page.RegisterOnReadyScript(BasePage.SCRIPTRUN_GRIDDATA,sb.toString());
	}
	
	public static String getLogBebinJs(BasePage page1,String cmd){
		if(!page1.isLogJs()) return "";
		StringBuilderEx sb = new StringBuilderEx();
		sb.Append("\r\n lg += ('"+cmd+" beging =' +(new Date().getTime()-time)/1000.0);");
		sb.Append("\r\n lg += '\\r\\n';");
		sb.Append("\r\n time = new Date().getTime();");
		return sb.toString();
	}
	
	public static String getLogEndJs(BasePage page1,String cmd){
		if(!page1.isLogJs()) return "";
		StringBuilderEx sb = new StringBuilderEx();
		sb.Append("\r\n lg += ('"+cmd+" end =' +(new Date().getTime()-time)/1000.0);");
		sb.Append("\r\n lg += '\\r\\n';");
		sb.Append("\r\n time = new Date().getTime();");
		return sb.toString();
	}
}
